package com.demo.进行工具类封装;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class 利用工具类进行模糊查询 {
    public static void main(String[] args) {
        //定义全局变量
        Connection con = null;
        PreparedStatement psta = null;
        ResultSet rs = null;
        //注册驱动，获取数据库链接
        try {
            con=Tool.getConnection();

            //获取预编译操作数据库对象
            String sql="select bName from books_4 where bName like ?";
            psta=con.prepareStatement(sql);
            psta.setString(1,"马%");

            //执行sql语句
            rs=psta.executeQuery();

            //处理结果集
            while(rs.next()){
                System.out.println(rs.getString(1));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //释放资源
            Tool.close(con,psta,rs);
        }
    }
}
